<?

function searchZip($term)
{
	$database = new mysqli('localhost', 'plotkinm', 'FaceBooK', 'plotkinm_db');

	$statement = "SELECT city, state FROM US_Zip_Codes WHERE city LIKE '".$term."%' ORDER BY city ASC";

	$results = $database->query($statement);

	$database->close();
	
	return $results;
}

function searchState($term)
{
	$database = new mysqli('localhost', 'plotkinm', 'FaceBooK', 'plotkinm_db');

	$statement = "SELECT state FROM US_Zip_Codes WHERE state LIKE '".$term."%' ORDER BY state ASC";

	$results = $database->query($statement);

	$database->close();
	
	return $results;
}


 
 $actions = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
 
 switch($actions)
 {
 	
 	case 'POST':
		
		
					break;
		
	default: //GET - Index & Details
	
	
					$state_results = searchState($_GET['term']);
					$zip_results = searchZip($_GET['term']);
					$array = array();
					while($state = $state_results->fetch_assoc())
					{
						
						if(!in_array($state,$array))
						{
							array_push($array, $city_state);
						}
						
					}
					
					while($zip = $zip_results->fetch_assoc())
					{
						$city_state = $zip['city'].", ".$zip['state'];
						
						
						if(!in_array($city_state,$array))
						{
							array_push($array, $city_state);
						}
						
					}
					
					echo json_encode($array);
 }

/*
	
 * 
 * function getZip()
{
	$database = new mysqli('localhost', 'plotkinm', 'FaceBooK', 'plotkinm_db');

	$statement = "SELECT city, state FROM US_Zip_Codes ORDER BY city ASC";

	$results = $database->query($statement);

	$database->close();
	
	return $results;
}
 * 
 * 
 * 					$zip_results = searchZip($_GET['term']);
					$array = array();
					while($zip = $zip_results->fetch_assoc())
					{
						$city_state = $zip['city'].", ".$zip['state'];
						
						$array[] = $city_state;
					}
					echo json_encode($array);
					$zip_results = getZip();
					$array = array();
					while($zip = $zip_results->fetch_assoc())
					{
						$city_state = $zip['city'].", ".$zip['state'];
						
						$array[] = $city_state;
					}
					echo json_encode($array);
 }
*/
?>